package com.py.customers.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.customers.domain.PyAccount;
import com.py.customers.mapper.BsCustomersMapper;
import com.py.customers.domain.BsCustomers;
import com.py.customers.service.IBsCustomersService;

import javax.annotation.Resource;

/**
 * 客户信息Service业务层处理
 * 
 * @author py
 * @date 2024-11-22
 */
@Service
public class BsCustomersServiceImpl implements IBsCustomersService 
{
    /**
     *
     */
    @Resource
    private BsCustomersMapper bsCustomersMapper;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public BsCustomers selectBsCustomersById(Long id)
    {
        return bsCustomersMapper.selectBsCustomersById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param bsCustomers 客户信息
     * @return 客户信息
     */
    @Override
    public List<BsCustomers> selectBsCustomersList(BsCustomers bsCustomers)
    {
        return bsCustomersMapper.selectBsCustomersList(bsCustomers);
    }

    /**
     * 新增客户信息
     * 
     * @param bsCustomers 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBsCustomers(BsCustomers bsCustomers)
    {
        int add=0;
        bsCustomers.setIsDeleted(0);//未删除
        System.out.println("带回家的："+bsCustomers);
        add=bsCustomersMapper.insertBsCustomers(bsCustomers);
        add=bsCustomersMapper.insertPyAccount(bsCustomers.getPyAccount());
        System.out.println(add);
        return add;
    }

    /**
     * 修改客户信息
     * 
     * @param bsCustomers 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBsCustomers(BsCustomers bsCustomers)
    {
        int update=0;
        bsCustomers.setIsDeleted(0);
        bsCustomers.setUpdateTime(DateUtils.getNowDate());
        update=bsCustomersMapper.updateBsCustomers(bsCustomers);
        update= bsCustomersMapper.uppdatePyaccount(bsCustomers.getPyAccount());
        System.out.println(update);
        return update;
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBsCustomersByIds(Long[] ids)
    {
//        bsCustomersMapper.deletePyAccountBySourceUnits(ids);
        return bsCustomersMapper.deleteBsCustomersByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBsCustomersById(Long id)
    {
        bsCustomersMapper.deletePyAccountBySourceUnit(id);
        return bsCustomersMapper.deleteBsCustomersById(id);
    }

    /**
     * 新增财务信息信息
     * 
     * @param bsCustomers 客户信息对象
     */
    public void insertPyAccount(BsCustomers bsCustomers)
    {
        List<PyAccount> pyAccountList = bsCustomers.getPyAccountList();
        Long id = bsCustomers.getId();
        if (StringUtils.isNotNull(pyAccountList))
        {
            List<PyAccount> list = new ArrayList<PyAccount>();
            for (PyAccount pyAccount : pyAccountList)
            {
                pyAccount.setSourceUnit(String.valueOf(id));
                list.add(pyAccount);
            }
            if (list.size() > 0)
            {
                bsCustomersMapper.batchPyAccount(list);
            }
        }
    }

    @Override
    public List<BsCustomers> selectAll() {
        return bsCustomersMapper.selectAll();
    }
}
